package cn.lingyangwl.agile.misc.short_link.listener;

import cn.lingyangwl.agile.misc.short_link.model.mq.*;
import cn.lingyangwl.agile.misc.short_link.service.*;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Slf4j
@Component
@RabbitListener(bindings = {
        @QueueBinding(
                value = @Queue(value = SlAccessLogSaveMQ.QUEUE, durable = "true"),
                exchange = @Exchange(value = SlAccessLogSaveMQ.EXCHANGE, type = ExchangeTypes.TOPIC))
})
public class SlAccessLogSaveListener {

    @Resource
    private AccessLogService accessLogService;

    @RabbitHandler
    public void onMessage(@Payload String msg, Channel channel, Message message) {
        log.info("消费删除用户消息, msg: {}", msg);
        SlAccessLogSaveMQ.MsgPayload userDelete = SlAccessLogSaveMQ.parse(msg, SlAccessLogSaveMQ.MsgPayload.class);
        accessLogService.saveAccessLog(userDelete.getAccessLog());
    }
}
